package com.ruoyi.common.utils;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.redis.RedisCache;
import lombok.SneakyThrows;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;

@Component
public class SendMessageUtil {

	private static SendMessageUtil po;

	@PostConstruct
	public void init() {
		po = this;
	}

	@Autowired
	private RedisCache redisCache;
	public static String VerificationCode(HttpServletRequest request,String mobile) throws IOException {
		HttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost("https://api.mix2.zthysms.com/v2/sendSms");
		httpPost.addHeader("Content-Type","application/json");
		String code = getrandom();
		request.setAttribute(mobile,code);
		Map<String,Object> map = new HashMap<>();
		po.redisCache.setCacheObject(mobile,code);
		long l = System.currentTimeMillis() / 1000;
		String adD0Uu0x = SecureUtil.md5(SecureUtil.md5("AdD0Uu0x") + l);
		map.put("username","15818201253");
		map.put("password",adD0Uu0x);
		map.put("tKey",l + "");
		map.put("mobile",mobile);
		map.put("content","【科源】您的验证码是："+code+"，5分钟内有效");
		String json = JSON.toJSONString(map);
		httpPost.setEntity(new StringEntity(json,"UTF-8"));
		HttpResponse response = httpClient.execute(httpPost);
		HttpEntity entity = response.getEntity();
		String res = EntityUtils.toString(entity);
		System.out.println(res);
		System.out.println("时间戳+："+ l);
		System.out.println("密码+："+ adD0Uu0x);
		return res;

	}

	public static String getrandom(){
		String code = "";
		Random random = new Random();
		for (int i = 0; i < 6; i++) {
			int r = random.nextInt(10); //每次随机出一个数字（0-9）
			code = code + r;  //把每次随机出的数字拼在一起
		}
		return code;

	}

	public static void main(String[] args) throws IOException {
		HttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost("http://api.mix2.zthysms.com/v2/sendSms");
		httpPost.addHeader("Content-Type","application/json");
		String code = getrandom();

		Map<String,Object> map = new HashMap<>();

		long l = System.currentTimeMillis() / 1000;
		String adD0Uu0x = SecureUtil.md5(SecureUtil.md5("AdD0Uu0x") + l);
		map.put("username","15818201253");
		map.put("password",adD0Uu0x);
		map.put("tKey",l);
		map.put("mobile","13316646932");
		map.put("content","【科源】您的验证码是："+code+"，5分钟内有效");
		String json = JSON.toJSONString(map);
		httpPost.setEntity(new StringEntity(json,"UTF-8"));
		HttpResponse response = httpClient.execute(httpPost);
		HttpEntity entity = response.getEntity();
		String res = EntityUtils.toString(entity);
		System.out.println(res);
		System.out.println(l);
		System.out.println(adD0Uu0x);
	}






}
